package com.ruoyi.informfrontend.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.informfrontend.domain.Article;
import com.ruoyi.informfrontend.domain.SysUser;
import com.ruoyi.informfrontend.mapper.SysUserMapper;
import com.ruoyi.informfrontend.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资讯Controller
 *
 * @author ruoyi
 * @date 2023-05-18
 */
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {
    @Autowired
    private IArticleService articleService;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询资讯列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Article article) {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 查询资讯列表
     */
    @GetMapping("/app")
    public AjaxResult app(Article article) {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return success(list);
    }


    /**
     * 获取资讯详细信息
     */
    @GetMapping(value = "/getOne")
    public AjaxResult getInfo(@RequestParam("id") Long id) {
        return success(articleService.selectArticleById(id));
    }

    @GetMapping(value = "/getMulti")
    public AjaxResult getMulti(@RequestParam("ids") Long[] ids) {
        return success(articleService.selectArticleByIds(ids));
    }

    /**
     * 新增资讯
     */
    @PostMapping
    public AjaxResult add(@RequestBody List<Article> articles) {
        SysUser user = userMapper.selectSysUserByUserId(SecurityUtils.getUserId());
        if (user == null) {
            return error("用户信息获取失败");
        }
        for (Article article : articles) {
            if (article.getId() == null) {
                article.setGovernId(SecurityUtils.getUserId());
                article.setReleaseName(user.getNickName());
                article.setArticleType("1");
                article.setLinkType(1L);
                articleService.insertArticle(article);
            } else if (article.getId() != null) {
                articleService.updateArticle(article);
            }

        }
        return success();
    }

    @GetMapping("/get")
    public AjaxResult get() {
        Article article = new Article();
        article.setGovernId(SecurityUtils.getUserId());
        article.setArticleType("1");
        List<Article> list = articleService.selectArticleList(article);
        return success(list);
    }

    /**
     * 修改资讯
     */
    @PutMapping
    public AjaxResult edit(@RequestBody List<Article> articles) {
        for (Article article : articles) {
            article.setGovernId(SecurityUtils.getUserId());
            articleService.updateArticle(article);
        }
        return success();
    }

    /**
     * 删除资讯
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(articleService.deleteArticleByIds(ids));
    }
}
